﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo.BusinessManager;
using DataInfo;
using KenLib.BusinessManager;
using System.Text.RegularExpressions;

public partial class Org_frmO_EmpRewPunInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((Org_MasterPage)this.Master).changeTitle("賞罰情報登録");
        ((Org_MasterPage)this.Master).HideLiArea("frmO_EmpRewPunInput");
        //Session["社員ID"] = "10";

        if (Session["社員ID"]==null || Session["社員ID"] == "")
            Response.Redirect("frmO_EmpWorkSkillInput.aspx");

        if (!Page.IsPostBack)
        {
            ClearTextPage();
            LoadMessageError();
        }
    }

    /// <summary>
    /// Load thong diep loi cho cac control Validate
    /// </summary>
    private void LoadMessageError()
    {
        RequiredFieldValidator1.ErrorMessage =  GetError("ER00001");
        RequiredFieldValidator2.ErrorMessage =  GetError("ER00001");
        //CustomValidator1.ErrorMessage = "賞罰年月日" + GetError("ER00001");
        RegularExpressionValidator1.ErrorMessage =  GetError("ER00002");
        RequiredFieldValidator3.ErrorMessage =  GetError("ER00001");
    }

    protected void btn_戻る_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmO_EmpWorkSkillInput.aspx");
    }

    protected void btn_次_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmO_EmpPhysicalExamInput.aspx");
    }

    protected void btnリセット_Click(object sender, EventArgs e)
    {
        ClearTextPage();
    }

    /// <summary>
    /// Xoa text o cac control
    /// </summary>
    private void ClearTextPage()
    {
        txt_賞罰特記事項.Text = "";
        txt_賞罰名.Text = "";
        txt_賞罰年月日.Text = "";
        txt_賞罰名.Focus();
    }

    protected void btn登録_Click(object sender, EventArgs e)
    {
        try
        {
            賞罰情報 obj = new 賞罰情報();
            obj.廃止区分 = 0;
            obj.賞罰名 = ConvertHTMLtoText(txt_賞罰名.Text.Trim());
            if (txt_賞罰年月日.Text != "")
            {
                DateTime d1 = DateTime.Parse(txt_賞罰年月日.Text);
                obj.賞罰年月日 = d1;
            }
            obj.賞罰特記事項 = ConvertHTMLtoText(txt_賞罰特記事項.Text);
            obj.社員ID = Int32.Parse(Session["社員ID"].ToString());


            int kq = new 賞罰情報Manager().InsertPartial(obj);
            if (kq != 0)
            {
                //ClearTextPage();
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "task", "clearText();", true);
                Response.Redirect("frmO_EmpPhysicalExamInput.aspx", false);
            }

        }
        catch (Exception ex) { }


    }

    /// <summary>
    /// Chuyen text thanh ma HTML
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertTexttoHTML(string temp)
    {
        temp = temp.Replace("<", "&lt;");
        temp = temp.Replace(">", "&gt;");
        return temp;
    }

    /// <summary>
    ///Chuyen ma HTML thanh text
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertHTMLtoText(string temp)
    {
        temp = temp.Replace("&lt;", "<");
        temp = temp.Replace("&gt;", ">");
        temp = temp.Replace(Convert.ToString((char)13), "<br/>");
        return temp;
    }

    /// <summary>
    /// Alert loi
    /// </summary>
    private void ErrorValid()
    {
        String str;
        str = "<script type='text/javascript'>";
        str += "alert('" + GetError("ER00002") + "')";
        str += "</script>";
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "err", str, false);
    }

    /// <summary>
    /// Alert loi
    /// </summary>
    private void ErrorRequest()
    {
        String str;
        str = "<script type='text/javascript'>";
        str += "alert('" + GetError("ER00001") + "')";
        str += "</script>";
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "err", str, false);
    }

    /// <summary>
    /// Lay ra loi can thiet ung voi ma loi
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        return dt.Rows[0][2].ToString();
    }

    protected Boolean checkDateValid(String input)
    {
        String pattern = @"^(?ni:(?=\d)((?'year'((1[6-9])|([2-9]\d))\d\d)(?'sep'[/.-])(?'month'0?[1-9]|1[012])\2(?'day'((?<!(\2((0?[2469])|11)\2))31)|(?<!\2(0?2)\2)(29|30)|((?<=((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)\2\3\2)29)|((0?[1-9])|(1\d)|(2[0-8])))(?:(?=\x20\d)\x20|$))?((?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}))?)$";
        return Regex.IsMatch(input, pattern);
    }
}